<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <canvas id="canvas" width="800" height="800"></canvas>
    <script>
      // 获取 canvas
      /** @type {HTMLCanvasElement} */
      const canvas = document.getElementById("canvas");
      // 获取上下文
      const ctx = canvas.getContext("2d");
      ctx.fillStyle = "red";
      // 绘制矩形
      ctx.fillRect(0, 0, 50, 50);
      ctx.beginPath();
      // 设置线宽
      ctx.lineWidth = 1;
      ctx.strokeStyle = "blue";
      ctx.moveTo(100, 100);
      ctx.lineTo(250, 75);
      ctx.lineTo(300, 100);
      // 必须调用stroke方法才能绘制图形
      ctx.stroke();

      // 结束上一段的绘制
      ctx.beginPath();
      ctx.lineWidth = 2;
      ctx.strokeStyle = "green";
      ctx.fillStyle = "red";
      ctx.arc(200, 200, 50, 0, 2 * Math.PI);
      ctx.stroke();
      ctx.fill();

      ctx.beginPath();
      ctx.lineWidth = 1;
      ctx.strokeStyle = "red";
      ctx.moveTo(300, 300);
      ctx.lineTo(301, 301);
      ctx.stroke()
    </script>
  </body>
</html>
